//
//  HistoryViewController.m
//  STL_ Animation
//
//  Created by 孙健 on 16/3/30.
//  Copyright © 2016年 STL. All rights reserved.
//

#import "HistoryViewController.h"
#import "AppDelegate.h"
#import "CoreDataManager.h"
#import "STL.h"
#import "MusicYue_V_TableViewCell.h"
#import "MusicYue_V_Model.h"
#import "PlayHistory.h"
#import "ChannelPageMV_ViewController.h"
#import "YMVViewController.h"

@interface HistoryViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) CoreDataManager *coreDataManager;
@property (nonatomic, strong) STLTableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSMutableArray *historyArr;
@property (nonatomic, strong) STLImageView *backImageView;

@end

@implementation HistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"播放历史";
    
    self.dataSource = [NSMutableArray arrayWithCapacity:0];
    self.historyArr = [NSMutableArray arrayWithCapacity:0];
    
    [self creatTableView];
    [self creatBackImageView];
    self.coreDataManager = [CoreDataManager shareCoreDataManager];
    NSLog(@"%@", [self.coreDataManager applicationDocumentsDirectory]);
    [self loadPlayHistoryDate];
    
}

- (void)loadPlayHistoryDate {
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"PlayHistory" inManagedObjectContext:self.coreDataManager.managedObjectContext];
    [fetchRequest setEntity:entity];
    // Specify criteria for filtering which objects to fetch
    
    NSError *error = nil;
    NSArray *fetchedObjects = [self.coreDataManager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        NSLog(@"%@", error);
    } else {
        [self.dataSource removeAllObjects];
        [self.historyArr removeAllObjects];
        
        for (PlayHistory *playHistory in fetchedObjects) {
            [self.historyArr addObject:playHistory];
            
            MusicYue_V_Model *model = [[MusicYue_V_Model alloc] init];
            model.posterPic = playHistory.listPic;
            model.title = playHistory.title;
            model.artistName = playHistory.artistName;
            
            model.hdUrl = playHistory.hdUrl;
            model.idName = playHistory.detailId;
            model.albumImg = playHistory.albumPic;
            
            [self.dataSource addObject:model];
        }
        
        if (self.dataSource.count == 0) {
            NSLog(@"没有播放历史");
            self.backImageView.hidden = NO;
        }
        [self.tableView reloadData];
    }
}

- (void)creatBackImageView {
    self.backImageView = [[STLImageView alloc] initWithFrame:CGRectMake(0, ScreenHeight * 0.045 + 5, ScreenWidth, 460)];
    self.backImageView.contentMode = UIViewContentModeScaleAspectFit;
    self.backImageView.image = [UIImage imageNamed:@"playHistory"];
    self.backImageView.hidden = YES;
    [self.view addSubview:self.backImageView];
}

#pragma mark - creatTableView
- (void)creatTableView {
    self.tableView = [[STLTableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 60) style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[MusicYue_V_TableViewCell class] forCellReuseIdentifier:@"MusicYue_V_TableViewCellIdentifier"];
    
    [self.view addSubview:self.tableView];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return ScreenWidth / 4;
}

#pragma mark - editingTableView
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"😄");
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        [self.dataSource removeObjectAtIndex:indexPath.row];
        
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
        
        [self.coreDataManager.managedObjectContext deleteObject:self.historyArr[indexPath.row]];
        
        [self.historyArr removeObjectAtIndex:indexPath.row];
        
        [self.coreDataManager saveContext];
    }
    
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MusicYue_V_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MusicYue_V_TableViewCellIdentifier"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.musicModel = self.dataSource[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PlayHistory *playHistory = self.historyArr[indexPath.row];
    MusicYue_V_Model *musicYueModel = self.dataSource[indexPath.row];
    
    if ([playHistory.type isEqualToString:@"channel"]) {
        
        ChannelPageMV_ViewController *channelPageMV_VC = [[ChannelPageMV_ViewController alloc] init];
        
        channelPageMV_VC.idName = [NSString stringWithFormat:@"%@", musicYueModel.idName];
        channelPageMV_VC.imageViewUrl = musicYueModel.albumImg;
        
        [self.navigationController pushViewController:channelPageMV_VC animated:YES];
    } else {
        YMVViewController *yMVVC = [[YMVViewController alloc] init];
        
        HomePageBaseModel *homeModel = [[HomePageBaseModel alloc] init];
        
        homeModel._id = musicYueModel.idName;
        
        yMVVC.homeModel = homeModel;
        [self.navigationController pushViewController:yMVVC animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
